﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cms.BLL.Controls
{
    public enum ControlType
    {
        None,
        Label,
        Button,
        Input,
        CheckBox,
        Radio,
        Panel,
        Combobox,//下拉
        Form,//表单
        TreeView,//树
        jqGrid,//表格
    }
    /// <summary>
    /// 控件样式
    /// </summary>
    public class ControlStyle
    {
        public int Width { get; set; }
        public int Height { get; set; }
        public int Left { get; set; }
        public int Top { get; set; }
        public ControlStyle():this(0,0) { }
        public ControlStyle(int width, int height, int left, int top) {
            this.Width = width;
            this.Height = height;
            this.Left = left;
            this.Top = top;
        }
        public ControlStyle(int width, int height) : this(width, height, 0, 0) { 
            
        }
    }
    public enum DataStoreType
    {
        ObjectModel,//后台实体
        DataBase//数据库
    }
    public interface IDataStore
    {
        /// <summary>
        /// 取数据
        /// </summary>
        /// <returns></returns>
        object Get();
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        bool Set();
    }
    /// <summary>
    /// 后台对象的方式处理数据
    /// </summary>
    public class ObjectDataStore : IDataStore
    {
        //对象在哪个文件中
        public string ClassName { get; set; }
        #region IDataStore 成员
        //要继承自一个接口.
        public object Get()
        {
            throw new NotImplementedException();
        }

        public bool Set()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
    public class DataBaseStore : IDataStore
    {
        #region IDataStore 成员

        public Model.Sys_Selfform SelfForm = null;
        public DataBaseStore(string formId)
        {
            BLL.Sys_Selfform bll = new Sys_Selfform();
            SelfForm = bll.GetById(formId, false);
        }
        public object Get()
        {
            //拼SQL
            throw new NotImplementedException();
        }

        public bool Set()
        {
            //拼Sql
            throw new NotImplementedException();
        }

        #endregion
    }

    /// <summary>
    /// 事件
    /// </summary>
    public class Event
    {
        /// <summary>
        /// 事件名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 事件体
        /// </summary>
        public string Body { get; set; }
        public Event(string name, string body)
        {
            this.Name = name;
            this.Body = body;
        }
    }
    public enum ValidType
    {
        None,//不验证
        Email,//电子邮件
        Tel,//电话
        Url,//地址
        Length,//长度
        Remote,//远程验证
        Custom//自定义
    }
    public class Validate
    {
        public Validate()
        {
            Type = ValidType.None;
        }
        public Validate(ValidType type, bool required)
        {
            Type = type;
            this.Required = required;
        }
        /// <summary>
        /// 是否必填
        /// </summary>
        public bool Required { get; set; }
        /// <summary>
        /// 验证类型
        /// </summary>
        public ValidType Type { get; set; }
        /// <summary>
        /// 自定义JS规则
        /// </summary>
        public string CustomRule { get; set; }
    }
    /// <summary>
    /// 控件基类
    /// </summary>
    public abstract class BaseControl
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public ControlStyle Style { get; set; }
        public ControlType Type { get; set; }
        public bool Enabled { get; set; }
        public List<Event> EventList = new List<Event>();
        public BaseControl()
        {
            this.Style = new ControlStyle();
            this.Type = ControlType.None;
            this.Enabled = true;
            this.Id = "";
            this.Name = "";
        }
    }
    public class FormControl : BaseControl
    {
        /// <summary>
        /// 表单编号
        /// </summary>
        public string FormId { get; set; }
        /// <summary>
        /// 表单的标题
        /// </summary>
        public string Caption { get; set; }

    }
    public class Label : BaseControl
    {
        public string Caption { get; set; }
        public Label()
        {
            this.Type = ControlType.Label;
            this.Style.Height = 16;
        }
    }
    public class Panel : BaseControl
    {
        public string Caption { get; set; }
        public Panel()
        {
            this.Type = ControlType.Panel;
            this.Style.Height = 50;
            this.Style.Width = 200;
        }
    }
    public class Button : BaseControl
    {
        public string Caption { get; set; }
        public Button()
        {
            this.Type = ControlType.Button;
            this.Style.Height = 16;
            this.EventList.Add(new Event("click", ""));
        }
    }
    public class Input : BaseControl
    {
        /// <summary>
        /// 值
        /// </summary>
        public string Value { get; set; }
        /// <summary>
        /// 数据验证
        /// </summary>
        public Validate DataValidate { get; set; }
        public Input()
        {
            this.Type = ControlType.Input;
            this.Style.Height = 16;
            this.Style.Width = 147;
            this.DataValidate = new Validate(ValidType.None,false);
        }
        
        public class Checkbox : BaseControl
        {
            public string Caption { get; set; }
            public bool Checked { get; set; }
            public Checkbox()
            {
                this.Type = ControlType.CheckBox;
                this.Style.Height = 16;
            }
        }
        public class Radio : BaseControl
        {
            public string Caption { get; set; }
            public bool Checked { get; set; }
            public Radio()
            {
                this.Type = ControlType.Radio;
                this.Style.Height = 16;
            }
        }
    }
}